import QtQuick 1.0
import "../Delegates"
import "../WebAccess/TwitterAPI.js" as TwitterAPI
import "../WebAccess/PictureAPI.js" as PictureAPI
import "../Util/Util.js" as Util

//つぶやき系データ用
ListView {
    id: _rootTimeline
    x: 0
    y: 0
    height: parent.height
    width: parent.width
    clip: true      //これをしないと下へスクロールする時にアクションバーに食い込む

    property bool loading: (count < 1)

    property bool more_new: contentY < 0  //上へ突き抜けた
    property bool more: (contentY + height) > contentHeight   //下へ突き抜けた


    signal slideInView     //表示した時のイベント

    onXChanged: {
        //最初は自動で読込み
        if((x === 0) && (count < 1)){
            model.getRecent();
        }

        slideInView();
    }

    delegate: Delegate{
        id: _delegate
        width: _rootTimeline.width

        itemIndex: _status_id
        itemId: makeItemId()
        itemTitle: makeItemTitle()
        itemMessage: makeItemMessage()
        itemInformation: makeItemInfomation()
        iconPath: isRetweet ? _rts_icon : _icon

        //保存用
        property bool isRetweet: (_rts_screen_name.length !== 0)
        property string screenName: isRetweet ? _rts_screen_name : _screen_name        //Screen name
        property string inReplyToStatusId: _in_reply_to_status_id
        property string inReplyToScreenName: _in_reply_to_screen_name

        property variant urlList: null      //URLのリスト

        //IDを成形する
        function makeItemId(){
            return _status_id;
        }

        //タイトルを成形する
        function makeItemTitle(){
            var ret = _screen_name + " / " + _name;
            if(isRetweet){
                ret = _rts_screen_name + " / " + _rts_name;
            }
            return ret;
        }

        //本文を成形する
        function makeItemMessage(){
            var text = isRetweet ? _rts_text : _text;

            //URLを正規表現で探す
            var re = new RegExp("http[s]*://[-_.!~*'()a-zA-Z0-9;/?:@&=+$,%#]+", "g");
            var urls = _urls.match(re);
            //画像があれば指定する
            PictureAPI.getPcitureUrl(urls, this);
            //URLを保存
            urlList = urls;

            //記号を戻す
            text = Util.decodeHtmlSymbol(text);

            return text;
        }

        //情報を成形する
        function makeItemInfomation(){
            var ret = _date;
            var dd = new Date(_date);

            ret = dd.getFullYear() + "/" + dd.getMonth() + "/" + dd.getDate();
            ret += " " + dd.getHours() + ":" + dd.getMinutes() + ":" + dd.getSeconds();

            if(_source.length !== 0){
                ret += " / " + _source;
            }
            if(inReplyToStatusId.length !== 0){
                ret += "<br />\nIn Reply To : " + inReplyToScreenName;
            }
            if(isRetweet){
                ret += "<br />\nRetweeted by : " + _screen_name;
            }

            return ret;
        }



        //リンクのクリック
        function handleLink(link){
            Qt.openUrlExternally(link);
        }

        MouseArea {
            anchors.fill: parent
            acceptedButtons: Qt.LeftButton | Qt.RightButton

            onClicked: {
                Util.outputDebugString("onClicked:" + pressedButtons
                                       + ",index=" + currentIndex
                                       + ",itemId=" + itemId);
                switch(itemIndex){
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    break;
                default:
                    break;
                }
//            }
//            onPressAndHold: {
//                Util.outputDebugString("onPressAndHold:" + pressedButtons);

                //情報を一時保存
                _selectItemInfo.itemId = itemId;
                _selectItemInfo.screenName = screenName;
                _selectItemInfo.message = itemMessage;
                _selectItemInfo.retweetMessage = "Do you do ReTweet of a remark of " + screenName;
                //メニューを出す
                _menuContainer.state = "MenuContextTimeline";
            }
        }
    }

    //読込中
    Image {
        anchors.centerIn: parent
        source: "../images/spinner.png"

        NumberAnimation on rotation {
            from: 0
            to: 360
            duration: 1500
            loops: Animation.Infinite
            running: loading
        }
        visible: loading
    }

}
